package com.topera.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.topera.commonutils.R;
import com.topera.vod.service.VodService;
import com.topera.vod.utils.ConstantPropertiesUtil;
import com.topera.vod.utils.VodUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/eduvod/video")
@CrossOrigin
public class VodController {

    @Autowired
    private VodService vodService;

    /**
     * 上传视频 到 阿里云
     * @param file
     * @return
     */
    @PostMapping("uploadVideo")
    public R uploadVideo(MultipartFile file){
        String videoId = vodService.uploadVodeo(file);
        return R.ok().data("videoId",videoId);
    }

    /**
     * 删除视频
     * @param videoSourceId
     * @return
     */
    @DeleteMapping("deleteVideo/{videoSourceId}")
    public R deleteVideo(@PathVariable String videoSourceId){
        vodService.deleteVideo(videoSourceId);
        return R.ok();
    }

    /**
     * 删除多个
     * @param videoIdList
     * @return
     */
    @DeleteMapping("deleteBatch")
    public R deleteBatch(@RequestParam("videoIdList") List videoIdList){
        vodService.deleteBatch(videoIdList);
        return R.ok();
    }

    /**
     * 获取视屏凭证
     * @param videoId
     * @return
     */
    @GetMapping("getPlayAuth/{videoId}")
    public R getPlayAuth(@PathVariable String videoId){

        String playAuth = "";
        try {

            DefaultAcsClient client = VodUtil.initVodClient(ConstantPropertiesUtil.ACCESS_KEY_ID, ConstantPropertiesUtil.ACCESS_KEY_SECRET);

            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            request.setVideoId(videoId);
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            playAuth = response.getPlayAuth();

        }catch (Exception e){

        }
        return R.ok().data("playAuth",playAuth);
    }

}
